getwd()Session 4
getwd()Navigating through the menus in RStudio is easy, (click and go) but requires using the menu every time the user runs the code.
Go to Session -> Set Working Directory. Two convenient options are:
Choose Directory…: Choose the directory yourself
To Source File Location: Set the working directory to the directory where your R Script (the source file) is saved
setwd("~/ownCloud (2)/Teaching/Rprogramming_UM/Rprogramming_UM")R interacts with files in several ways.
Datasets can come in different formats.
X STNID NAME CTRY
Min. : 87132 Length:2896 Length:2896 Length:2896
1st Qu.: 88219 Class :character Class :character Class :character
Median : 92226 Mode :character Mode :character Mode :character
Mean : 93638
3rd Qu.: 96234
Max. :100154
COUNTRY_NAME ISO2C ISO3C LATITUDE
Length:2896 Length:2896 Length:2896 Min. :50.91
Class :character Class :character Class :character 1st Qu.:50.91
Mode :character Mode :character Mode :character Median :51.18
Mean :51.18
3rd Qu.:51.45
Max. :51.45
LONGITUDE ELEVATION BEGIN END
Min. :5.375 Min. : 22.55 Min. :19490101 Min. :20240909
1st Qu.:5.375 1st Qu.: 22.55 1st Qu.:19490101 1st Qu.:20240909
Median :5.572 Median : 68.42 Median :19490101 Median :20240909
Mean :5.572 Mean : 68.42 Mean :19490101 Mean :20240909
3rd Qu.:5.770 3rd Qu.:114.30 3rd Qu.:19490101 3rd Qu.:20240909
Max. :5.770 Max. :114.30 Max. :19490101 Max. :20240909
YEARMODA YEAR MONTH DAY
Length:2896 Min. :2021 Min. : 1.000 Min. : 1.00
Class :character 1st Qu.:2021 1st Qu.: 4.000 1st Qu.: 8.00
Mode :character Median :2022 Median : 7.000 Median :16.00
Mean :2022 Mean : 6.523 Mean :15.71
3rd Qu.:2023 3rd Qu.:10.000 3rd Qu.:23.00
Max. :2024 Max. :12.000 Max. :31.00
YDAY TEMP TEMP_ATTRIBUTES DEWP_ATTRIBUTES
Min. : 1.00 Min. :-8.50 Min. : 7.00 Min. : 7.00
1st Qu.: 92.75 1st Qu.: 6.90 1st Qu.:24.00 1st Qu.:24.00
Median :183.00 Median :11.45 Median :24.00 Median :24.00
Mean :183.10 Mean :11.69 Mean :23.88 Mean :23.88
3rd Qu.:274.00 3rd Qu.:16.80 3rd Qu.:24.00 3rd Qu.:24.00
Max. :366.00 Max. :30.00 Max. :24.00 Max. :24.00
SLP_ATTRIBUTES STP_ATTRIBUTES VISIB_ATTRIBUTES WDSP_ATTRIBUTES
Min. : 0.00000 Min. : 0.00000 Min. : 7.00 Min. : 7.00
1st Qu.: 0.00000 1st Qu.: 0.00000 1st Qu.:24.00 1st Qu.:24.00
Median : 0.00000 Median : 0.00000 Median :24.00 Median :24.00
Mean : 0.03729 Mean : 0.03729 Mean :23.87 Mean :23.88
3rd Qu.: 0.00000 3rd Qu.: 0.00000 3rd Qu.:24.00 3rd Qu.:24.00
Max. :13.00000 Max. :13.00000 Max. :24.00 Max. :24.00
MAX I_FOG I_RAIN_DRIZZLE I_SNOW_ICE
Min. :-5.00 Min. :0.0000 Min. :0.0000 Min. :0.00000
1st Qu.:10.20 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.00000
Median :16.00 Median :0.0000 Median :1.0000 Median :0.00000
Mean :15.94 Mean :0.1381 Mean :0.6454 Mean :0.04385
3rd Qu.:22.00 3rd Qu.:0.0000 3rd Qu.:1.0000 3rd Qu.:0.00000
Max. :39.50 Max. :1.0000 Max. :1.0000 Max. :1.00000
I_HAIL I_THUNDER I_TORNADO_FUNNEL ES
Min. :0.000000 Min. :0.00000 Min. :0 Min. :0.300
1st Qu.:0.000000 1st Qu.:0.00000 1st Qu.:0 1st Qu.:1.000
Median :0.000000 Median :0.00000 Median :0 Median :1.350
Mean :0.004489 Mean :0.07562 Mean :0 Mean :1.478
3rd Qu.:0.000000 3rd Qu.:0.00000 3rd Qu.:0 3rd Qu.:1.900
Max. :1.000000 Max. :1.00000 Max. :0 Max. :4.200
data <- load("data/climate_Maas_Eind.Rdata")Option 1: Using menus within RStudio is the easiest (click and go) but requires using the menu every time the user runs the code.
Option 1: Using menus within RStudio (cont’d)
Option 1: Using menus within RStudio (cont’d)
Option 1: Using menus within RStudio (cont’d)
Advice for option 1:
Advice for option 1 (cont’d):
library(readxl)
climate <- read_excel("data/climate.xlsx")import("data/climate.csv")save() saves objects as an .RData file.save.image() saves a selection of objects as an .RData file.save.image().save().data_short, and save this list using function save.
To create plots with R’s standard graphics package, there are high-level and low-level plotting functions.
Notice that function plot() calls methods.
It will perform different operations depending on the class of the passed object. (We study the lm() function in detail in the next session!)
The plot() function takes several many arguments that can change the layout of the plots. See ?par for all graphical options; there are many!
Some examples:
col: color of lines / pointslty, lwd: Line type and thicknesspch: Point type (1-16)main, sub: Title, subtitlexlab, ylab: x and y axis labelslog, xlog and ylog for logarithmic scalesxlim, ylim: x and y axis limits (for overriding R’s default choices)mfcol, mfrow: Multiple plots in one graphics window (column-wise/row-wise)lines: Draw linesabline: Quickly add horizontal, vertical lines, and lines using equation \(y = bx + a\)
points: Add pointsarrows: Add arrowstitle: Add a titlelegend: Add a legendtext: Add text at \((x,y)\) coordinatesmtext: Add text with positional specification like side=1,...,4
We want to visualize the daily maximum temperatures in the climate data data_short specifically for Maastricht. First, make a basic plot of variable and MAX then customise the plot in the following ways:
The title of the X-axis should say ‘Year’, the title of the Y-axis ‘Maximum Temperature’.
Make the plot a line plot with a blue line. (Hint: specifying the colour literally as "blue" works)
Make the tick marks appear on the inside of the figure rather than the outside.
Calculate the average temperature.
Add a horizontal line with the average maximum temperature
You will need to consult the help file for this exercise; see this therefrom more as an exercise in how to navigate R’s help system, than an exercise in plotting (which we will cover in more detail later).
You may want to ask ChatGPT for help and then try to see if you could also have gotten the same answer yourself; it may not always give you the most straightforward answer though!
plot(x = data_short$YEAR, y = data_short$MAX) You can manually save graphs of several formats.
Best practice is to save a graph through a device such as pdf or similar: